<?php


/**
 * CodeMarker is a factory class built up for returning 
 * 
 * @category    Waveself
 * @package     CodeMarker
 * @author      Nevio Vesic <nevio.vesic@gmail.com>
 * @copyright   Waveself 2009-2010. 
 * @version     $id: 
 */
class Waveself_CodeMarker extends Waveself_CodeMarker_Abstract
{
	/**
	 * Current, CodeMarker component siglenton instance
	 *
	 * @var NULL|Waveself_CodeMarker
	 */
	protected static $_instance = null;
	
	/**
	 * Create CodeMarker siglenton instance.
	 *
	 * @return Waveself_CodeMarker
	 */
	public static function getInstance()
	{
		if (!isset(self::$_instance)) self::$_instance = new self;
		return self::$_instance;
	}
	
	/**
	 * Call CodeMarker factory adapters or retrieve from siglenton instance!
	 *
	 * @param    String                             $adapter
	 * @param    Object|Array                       $options
	 * @return   Waveself_CodeMarker_Adapters_*
	 */
	public static function factory($adapter, $options=null) 
	{
		/** First let's prepare options if there exist any... */
		if (isset($options)) {
			if (!is_array($options) && is_object($options)) {
				$options = (array) $options;
			}
		}
		
		/** Now let's go checkout and prepare language adapter */
		if (!isset($adapter)) {
			 throw new Waveself_Exception('You need to choose adapter in order to load marker! Load failed!');
		}
		
		$adapter = trim(ucfirst($adapter));
		$class   = self::CLASS_PREFIX . $adapter;
		
		if (!isset(self::$_adapters[strtolower($adapter)])) {
			self::$_adapters[strtolower($adapter)] = new $class($options);
		}
		
		$cE = class_parents(self::$_adapters[strtolower($adapter)]);
		$cI = class_implements(self::$_adapters[strtolower($adapter)]);
		
		if (!in_array('Waveself_CodeMarker_Abstract', $cE) || !in_array('Waveself_CodeMarker_Adapters_Interface', $cI)) {
			throw new Waveself_Exception('You need to extend adapter with proper abstract class and inteface!');
		}
		
		return self::$_adapters[strtolower($adapter)];
	}
}